﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using US.Business;
using US.Entity;
using US.Utilities;

namespace US.WebApp.Areas.CommonModule.Controllers
{
    /// <summary>  
    /// 海关编码控制器  
    /// </summary>  
    public class BmsCustomItemController : PublicController<Bms_Custom_Item>
    {
        Bms_Custom_ItemBll bms_Custom_ItemBll = new Bms_Custom_ItemBll();

        private Base_HX_DataDictionaryBll Base_HX_DataDictionarybll = new Base_HX_DataDictionaryBll();

        #region 获取列表

        /// <summary>
        /// 【海关编码】返回列表JONS
        /// </summary>
        /// <returns></returns>
        public ActionResult ListJson()
        {
            List<Bms_Custom_Item> list = bms_Custom_ItemBll.GetList();
            return Content(list.ToJson());
        }

        //public virtual JsonResult GridPageJson(string ParameterJson, JqGridParam jqgridparam)

        /// <summary>
        /// 分页获取海关编码列表
        /// </summary>
        /// <param name="hsCode">海关编码</param>
        /// <param name="itemName">商品名称</param>
        /// <param name="applyYyyy">适用年份</param>
        /// <returns></returns>
        public JsonResult GridListPageJson(string hsCode, string itemName, string applyYyyy, JqGridParam jqgridparam)
        {
            Stopwatch watch = CommonHelper.TimerStart();
            List<Bms_Custom_Item> ListData = bms_Custom_ItemBll.GridListJson(hsCode, itemName, applyYyyy, ref jqgridparam);
            var JsonData = new
            {
                total = jqgridparam.total,
                page = jqgridparam.page,
                records = jqgridparam.records,
                costtime = CommonHelper.TimerEnd(watch),
                rows = ListData,
            };
            return Json(JsonData, JsonRequestBehavior.AllowGet);
            //return Content(list.ToJson());
        }

        #endregion

        /// <summary>
        /// 返回数据字典列表JONS
        /// </summary>
        /// <returns></returns>
        public JsonResult bindDataList(string code)
        {
            List<Base_HX_DataDictionaryDetail> list = Base_HX_DataDictionarybll.GetBase_HX_DataDictionaryDetailListByCodeNew(code);
            return Json(list, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 返回已有适用年限
        /// </summary>
        /// <returns></returns>
        public JsonResult bindYYYYList(string code)
        {
            DataTable dt = bms_Custom_ItemBll.GetYYYYList();

            var list=dt.AsEnumerable().Select(t=> t.Field<string>("YYYY")).ToList();
            return Json(list, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 复制视图
        /// </summary>
        /// <returns></returns>
        [ManagerPermission(PermissionMode.Enforce)]
        public virtual ActionResult Copy()
        {
            return View();
        }


        /// <summary>
        /// 复制海关编码
        /// </summary>
        /// <param name="yyyyOld">实体对象</param>
        /// <param name="yyyyNew">主键值</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        [LoginAuthorize]
        public ActionResult SubmitCopy(int yyyyOld, int yyyyNew)
        {
            try
            {
                bms_Custom_ItemBll.SubmitCopy(yyyyOld, yyyyNew);
                this.WriteLog(1, null, null, yyyyOld.ToString(), "复制成功。");
                return Content(new JsonMessage { Success = true, Code = "1", Message = "复制成功。" }.ToString());
            }
            catch (Exception ex)
            {
                this.WriteLog(-1, null, null, yyyyNew.ToString(), "操作失败：" + ex.Message);
                return Content(new JsonMessage { Success = false, Code = "-1", Message = "操作失败：" + ex.Message }.ToString());
            }


        }
    }
}
